﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using WCrawlerLib;

namespace WCrawler.Controls
{
    public partial class ucTreeViewTopic : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                this.treeViewTopic.Nodes[0].Selected = true;
        }

        public TreeNode SelectedNode
        {
            get
            {
                return this.treeViewTopic.SelectedNode;
            }
        }

        public void PopulateRootTopics(TreeNode node)
        {
            Topic objTopic = new Topic();
            var query = objTopic.GetRootTopics();
            foreach (Topic value in query)
            {
                TreeNode newNode = new TreeNode(value.TopicName, value.TopicId.ToString());
                var childNodes = objTopic.GetChildTopics(value.TopicId);
                newNode.PopulateOnDemand = true;
                newNode.SelectAction = TreeNodeSelectAction.Select;
                node.ChildNodes.Add(newNode);
            }

        }

        public void PopulateChildTopics(TreeNode node)
        {
            Topic objTopic = new Topic();
            var query = objTopic.GetChildTopics(int.Parse(node.Value));
            foreach (Topic value in query)
            {
                TreeNode newNode = new TreeNode(value.TopicName, value.TopicId.ToString());
                var childNodes = objTopic.GetChildTopics(value.TopicId);
                newNode.PopulateOnDemand = false;
                newNode.SelectAction = TreeNodeSelectAction.Select;
                node.ChildNodes.Add(newNode);
            }
            
        }

        protected void treeViewTopic_TreeNodePopulate(object sender, TreeNodeEventArgs e)
        {
            if (e.Node.ChildNodes.Count == 0)
            {
                switch (e.Node.Depth)
                {
                    case 0:
                        PopulateRootTopics(e.Node);
                        //e.Node.Selected = true;
                        break;
                    default:
                        PopulateChildTopics(e.Node);
                        break;
                }
            }
        }

        /// <summary>
        /// Get all topics in selected node
        /// </summary>
        /// <param name="parentNode"></param>
        /// <returns></returns>
        public string GetTopics(TreeNode parentNode)
        {
            string query = parentNode.Value;
            List<Topic> topicList = new List<Topic>();
            Topic objTopic = new Topic();
            if (parentNode.Parent != null)
                objTopic.GetAllChildTopics(int.Parse(parentNode.Value), ref topicList);
            else
                topicList = objTopic.GetAllTopics().ToList();

            foreach (Topic obj in topicList)
            {
                query += "|" + obj.TopicId.ToString();
            }

            return query;

            //string query = parentNode.Value;
            //TreeNodeCollection nodeCollection = parentNode.ChildNodes;
            //foreach (TreeNode node in nodeCollection)
            //{
            //    query += "|" + GetTopics(node);
            //}
            //return query;
        }

        public event EventHandler BubbleIndexChanged;
        protected void treeViewTopic_SelectedNodeChanged(object sender, EventArgs e)
        {
            if (treeViewTopic.SelectedNode.ChildNodes.Count == 0)
            {
                treeViewTopic.SelectedNode.PopulateOnDemand = true;
                treeViewTopic.SelectedNode.SelectAction = TreeNodeSelectAction.Select;
                treeViewTopic.SelectedNode.Expand();
            }
            if (BubbleIndexChanged != null)
                BubbleIndexChanged(sender, e);
        }
    }
}